package com.hnjfzn.mall.controller;
import com.hnjfzn.common.core.web.domain.AjaxResult;
import com.hnjfzn.mall.domain.ProductCategory;
import com.hnjfzn.mall.domain.UserAddress;
import com.hnjfzn.mall.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user-addresses")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @PostMapping("/add")
    public AjaxResult addUserAddress(@RequestBody UserAddress userAddress) {
        boolean result = userAddressService.addUserAddress(userAddress);
        return result ? AjaxResult.success() : AjaxResult.error();
    }

    @GetMapping("/user/{userId}")
    public AjaxResult getUserAddressesByUserId(@PathVariable Long userId) {
        List<UserAddress> userAddresses = userAddressService.getUserAddressesByUserId(userId);
        return AjaxResult.success(userAddresses);
    }

    @GetMapping("/{addressId}")
    public AjaxResult getUserAddress(@PathVariable Long addressId) {
        return AjaxResult.success(userAddressService.getUserAddress(addressId));
    }

    @PutMapping("/{addressId}")
    public AjaxResult updateUserAddress(@PathVariable Long addressId, @RequestBody UserAddress userAddress) {
        userAddress.setAddressId(addressId);
        boolean result = userAddressService.updateUserAddress(userAddress);
        return result ? AjaxResult.success() : AjaxResult.error();
    }

    @DeleteMapping("/{addressId}")
    public AjaxResult deleteUserAddress(@PathVariable Long addressId) {
        boolean result = userAddressService.deleteUserAddress(addressId);
        return result ? AjaxResult.success() : AjaxResult.error();
    }
}